'use strict'

const bcrypt = require('bcrypt-promise')
// const fs = require('fs-extra')
// const mime = require('mime-types')
const sqlite = require('sqlite-async');
// const query = require('../config/connection');
const saltRounds = 10

module.exports = class TeachMaterial {

	constructor(dbName = 'website.db') {
		return (async() => {
			this.db = await sqlite.open(dbName)
			return this
		})()
	}

	async listALl() {
		try {
			let sql = `SELECT * FROM teachmaterials;`
			const data = await this.db.all(sql)
			return data
		} catch(err) {
			throw err
		}
	}

	async getOne(id) {
		try {
			let sql = `SELECT * FROM teachmaterials where id = ${id};`
			const data = await this.db.get(sql)
			return data
		} catch(err) {
			throw err
		}
	}

	async add(name) {
		try {
			sql = `INSERT INTO teachmaterials(name) VALUES("${name}")`
			await this.db.run(sql)
			return true
		} catch(err) {
			throw err
		}
	}

	async delete(id) {
		try {
			if(id <= 0) throw new Error('id must be greater then 0')
			let sql = `delete FROM teachmaterials WHERE id="${id}";`
			await this.db.run(sql)
			return true
		} catch(err) {
			throw err
		}
	}
}
